from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.clickjacking import xframe_options_sameorigin
from django.views.decorators.csrf import csrf_exempt
# Create your views here.

from common.flow import *


transports = None

flow = {
    # 环境
    "dev": update_env,
    "yf": update_env,
    "connect_sql": connect_sql,
    # 登录
    "pc_login": pc_login,
    "app_login": app_login,
    # 获取数据
    "get_store_info": get_store_info,
    "get_dispatching_rule": get_dispatching_rule,
    "get_treaty_config": get_treaty_config,
    "get_cargo": get_cargo,
    # 销售订单
    "sales_order_query": sales_order_query,
    # 物料配置
    "set_cargo": set_cargo,
    # 启用一级合约
    "set_one_hy_rule": set_one_hy_rule,
    # 启用二级合约
    "set_two_hy_rule": set_two_hy_rule,
    # 设置合约地址
    "set_hy_address": set_hy_address,
    # 设置配送地址
    "set_ps_address": set_ps_rule,
    # 销售订单重新判断
    "sales_reset": sales_reset,
    # 获取手机号
    "get_driver_iphone": get_driver_iphone,
    # 短途预约
    "driver_bespeak": driver_bespeak,
    # 短途预约审核
    "baspeak_audit": baspeak_audit,
    # 短途预约签到
    "driver_sign": driver_sign,
    # 订单分配
    "sales_order_allot": sales_order_allot,

    # 退回操作
    "untread_order_audit": untread_order_audit,
    "get_untread_transport_order": get_untread_transport_order,
    # 预售
    "presell_plan_distribution": presell_plan_distribution,
    "presell_plan_retrieve": presell_plan_retrieve,
    "presell_plan_force_distribution": presell_plan_force_distribution,
    "presell_plan_force_retrieve": presell_plan_force_retrieve,

    # 物流单流程
    "logistics_order_query": logistics_order_query,
    "get_logistics_order": get_logistics_order,
    "create_transport_order": create_transport_order,
    "get_logistics_transport_order": get_logistics_transport_order,
    # 装车单流程
    "transport_order_query": transport_query,
    "allot": transport_allot,
    "challenge": transport_challenge,
    "into_factory": transport_into_factory,
    "first_loadometer": first_loadometer,
    "loading": loading,
    "inspect": inspect,
    "two_loadometer": two_loadometer,
    "two_loadomter_hand_movement": two_loadomter_hand_movement,
    "out_of_factory": out_of_factory,
    "transport_order_arrival": transport_order_arrival,
    "transport_order_signfor": transport_order_signfor,
    # "into_factory2": into_factory2,
    # "first_loadometer2": first_loadometer2,
    # "loading2": loading2,
    # "inspect2": inspect2,
    # "two_loadometer2": two_loadometer2,
    # "out_of_factory2": out_of_factory2,
    # 冻结流程
    "freeze": freeze,
    "freeze_audio": freeze_audio,
    "freeze_affirm": freeze_affirm,
    "freeze_stowage_cancel": freeze_stowage_cancel,
    "freeze_loadometer": freeze_loadometer,
    # 退货流程
    "return_app_sign": return_app_sign,
    "return_into_factory": return_into_factory,
    "return_first_loadometer": return_first_loadometer,
    "return_loading": return_loading,
    "return_inspect": return_inspect,
    "return_two_loadometer": return_two_loadometer,
    "return_out_of_factory_audit": return_out_of_factory_audit,
    "return_out_ofFactory": return_out_of_factory
}


# def sales_order_query(request):

#     order_no = request.POST.get('order_no')
#     print(f"order_no: {order_no}")
#     res_status = "功能待实现"
#     return res_status


# def transport_order_query(request):
#     global transports
#     transport_no = request.POST.get('order_no')
#     transports = TransportOrder(transport_no)
#     return transports.status


# def allot(request):
#     global transports
#     transport_no = request.POST.get('order_no')
#     if not transports:
#         transports = TransportOrder(transport_no)

#     transports.transport_order_allot()
#     return transports.status


# def challenge(request):
#     global transports
#     transport_no = request.POST.get('order_no')
#     if not transports:
#         transports = TransportOrder(transport_no)

#     transports.transport_order_challenge()
#     return transports.status

@xframe_options_sameorigin
@csrf_exempt
def tmsFlowView(request):
    # 定义全局变量orders
    global orders
    # 获取请求中的type参数
    if request.method == "POST":
        optype = request.POST.get('type', None)
        print(f"optype: {optype}")
        res_status = None
        if optype:
            func = flow.get(optype, None)
            if func:
                res_status = func(request)
            else:
                return JsonResponse({
                    "status": "操作类型不存在"
                })
        return JsonResponse(res_status)

    return render(request, 'tms_flow/tms_flow.html')
